package com.ruoyi.wechat.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.google.zxing.WriterException;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.wechat.domain.Camp;
import com.ruoyi.wechat.mapper.CampMapper;
import com.ruoyi.wechat.mapper.OrderDetailMapper;
import com.ruoyi.wechat.service.ICampService;
import com.ruoyi.wechat.service.IGoodsService;
import com.ruoyi.wechat.service.IOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wechat.domain.OrderDetail;
import com.ruoyi.wechat.mapper.OrderMapper;
import com.ruoyi.wechat.domain.Order;
import com.ruoyi.wechat.service.IOrderService;

/**
 * 订单管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-30
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private CampMapper campMapper;

    /**
     * 查询订单管理
     * 
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Override
    public Order selectOrderByOrderId(String orderId)
    {
        return orderMapper.selectOrderByOrderId(orderId);
    }

    /**
     * 查询订单管理列表
     * 
     * @param order 订单管理
     * @return 订单管理
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增订单管理
     *
     * @param order 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrder(Order order) throws IOException, WriterException {
        order.setOrderId(IdUtils.randomUUID());
        Map<String, Object> map = goodsService.generate(order.getOrderId());
        order.setQrcode((String) map.get("data"));
        order.setOrderTime(new Date());
        for (int i= 0;i<order.getOrderDetailList().size();i++){
            order.getOrderDetailList().get(i).setOrderId(order.getOrderId());
            order.getOrderDetailList().get(i).setOrderDetailId(IdUtils.randomUUID());
            orderDetailMapper.insertOrderDetail(order.getOrderDetailList().get(i));
        }
        return orderMapper.insertOrder(order);
    }

    /**
     * 新增订单管理
     * 
     * @param order 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public String insertOrderCopy(Order order) throws IOException, WriterException {
        order.setOrderId(IdUtils.randomUUID());
        Map<String, Object> map = goodsService.generate(order.getOrderId());
        order.setQrcode((String) map.get("data"));
        order.setOrderTime(new Date());
        for (int i= 0;i<order.getOrderDetailList().size();i++){
            order.getOrderDetailList().get(i).setOrderId(order.getOrderId());
            order.getOrderDetailList().get(i).setOrderDetailId(IdUtils.randomUUID());
            orderDetailMapper.insertOrderDetail(order.getOrderDetailList().get(i));
        }
        orderMapper.insertOrder(order);
        return order.getOrderId();
    }

    /**
     * 修改订单管理
     * 
     * @param order 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrder(Order order)
    {
        orderMapper.deleteOrderDetailByOrderId(order.getOrderId());
        insertOrderDetail(order);
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单管理
     * 
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderByOrderIds(String[] orderIds)
    {
        orderMapper.deleteOrderDetailByOrderIds(orderIds);
        return orderMapper.deleteOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单管理信息
     * 
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderByOrderId(String orderId)
    {
        orderMapper.deleteOrderDetailByOrderId(orderId);
        return orderMapper.deleteOrderByOrderId(orderId);
    }

    @Override
    public String getqr(Order order) {
        List<Order> orders = orderMapper.selectOrderList(order);
        String qrCode = null;
        if (orders!=null) {
            qrCode = orders.get(0).getQrcode();
            return qrCode;
        }
        return "无效订单号";
    }

    @Override
    public List<Order> getByUserIdList(String userId) {
        //根据用户id查询名下所有的营地id
        String[] campIds = campMapper.selectUserId(userId);

        //根据营地id查询所有的订单信息
        List<Order> list = orderMapper.selectOrderByUserId(campIds);
        for (int i = 0; i < list.size(); i++) {
            Order order = list.get(i);
            List<OrderDetail> orderDetailList = orderDetailMapper.selectOrderIdDetailList(list.get(i).getOrderId());
            order.setOrderDetailList(orderDetailList);
        }
        return list;
    }

    @Override
    public int deOrderSatus(String orderId) {
        return orderMapper.delOrderStatus(orderId);
    }

    @Override
    public int saoyisao(String orderId, String userId) {
        //先根据订单id查询营地id
        String campId =  orderMapper.selectOrderByCampId(orderId);
        //根据营地id查询出用户id
        Camp camp = campMapper.selectCampByCampId(campId);
        //如果一样就执行修改语句
        if (camp.getUserId().equals(userId)){
            return orderMapper.updateStatus(orderId);
        }
        //否则就报错
        return 0;

    }

    /**
     * 新增订单详情信息
     *
     * @param order 订单管理对象
     */
    public void insertOrderDetail(Order order)
    {
        List<OrderDetail> orderDetailList = order.getOrderDetailList();
        String orderId = order.getOrderId();
        if (StringUtils.isNotNull(orderDetailList))
        {
            List<OrderDetail> list = new ArrayList<OrderDetail>();
            for (OrderDetail orderDetail : orderDetailList)
            {
                orderDetail.setOrderId(orderId);
                list.add(orderDetail);
            }
            if (list.size() > 0)
            {
                orderMapper.batchOrderDetail(list);
            }
        }
    }
}
